var follow_path_fly;
$('#routefly').on('click', routeFly)
function routeFly(){
    $('#routefly').addClass('hide');
    $('#stopfly').addClass('hide');
    $('#floorclose').addClass('hide');
    clearInterval(path_fly);
    clearInterval(path_fly_2);
    //清除重复点击的跟随漫游
        //存在崩溃问题
    clearInterval(follow_path_fly);
    var direction = null;
    var distance = null;
    //视角方向设置
    if(escapePoints[1].y>escapePoints[0].y){
        direction = 0
        distance = -0.00005;
    }
    if(escapePoints[1].y<escapePoints[0].y){
        direction = Math.PI
        distance = 0.00005;
    }
    //初始视角
    viewer.camera.setView({
        destination:Cesium.Cartesian3.fromDegrees(
            components[0].x,
            components[0].y + distance,
            64
            ),
    orientation:{
        heading:direction + Math.atan(
            (components[routeLength-1].x-components[0].x)/(components[routeLength-1].y-components[0].y)
            ),
        pitch:-0.2,
        roll:0  
    },
    // duration:2
    })
     //跟随漫游
    follow_path_fly = setTimeout(function(){
        var m = 0;
        pTimeId_2 = setInterval(function(){
        //抵达终点
            if (m == routeLength-5){
                clearInterval(pTimeId_2); 
            //恢复操控界面
                $('#stopfly').removeClass('hide');
                $('#floorclose').removeClass('hide');
                $('#routefly').removeClass('hide');
            //自动回到初始视角
                setTimeout(function(){
                    viewer.camera.flyTo({
                        destination:Cesium.Cartesian3.fromDegrees(
                            components[0].x,
                            components[0].y + distance,
                            64
                            ),
                        orientation:{
                            heading:direction + Math.atan(
                                (components[routeLength-1].x-components[0].x)/(components[routeLength-1].y-components[0].y)
                                ),
                            pitch:-0.2,
                            roll:0  
                        },
                        duration:2
                    })
                },1500)
            }
            //动画效果
            viewer.camera.setView({
                destination:Cesium.Cartesian3.fromDegrees(
                    components[m].x ,
                    components[m].y + distance,
                    64
                    ),
            orientation:{
                heading:direction + Math.atan(
                    (components[routeLength-1].x-components[m].x)/(components[routeLength-1].y-components[m].y)
                    ),
                pitch:-0.2,
                roll:0  
            }})
            m++;

        }, 20)
    }, 3500);

      
}
